{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. Mathematical Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Functions that return true or false (boolean values)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.1 isPrime" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_1\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_1\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_1\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_1\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #1:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #1 state: Loading...
\n", "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "boolean isPrime(int number) {\n", " // if number is evenly divided by any number less than number\n", " // (except 1) then it is prime\n", " // otherwise, not prime\n", " for (int i = 2; i < number; i++) {\n", " if ((number % i) == 0) {\n", " return false;\n", " }\n", " }\n", " return true;\n", " \n", "}\n", "\n", "void setup() {\n", " println(\"isPrime(100): \" + isPrime(100));\n", " println(\"isPrime(101): \" + isPrime(101));\n", " println(\"isPrime(107): \" + isPrime(107));\n", "}" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## 1.2 GCD" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Greatest common divisor: the largest positive integer that divides all of the numbers without a remainder. For example, the GCD of 8 and 12 is 4.\n", "\n", "Euclid's Algorithm (after https://en.wikipedia.org/wiki/Greatest_common_divisor):\n", "\n", "1. Find the GCD of a and b\n", "1. If a or b is zero, then return the other\n", "1. if a > b, set a to a - b\n", "1. if b > a, set b to b - a\n", "1. go to step 2\n", "\n", "Example:\n", "\n", "
\n",
    "1. Let a = 8, and b = 12\n",
    "2. Neither is zero\n",
    "3. a = 4 (12 - 8), b = 12\n",
    "4. a = 4, b = 8 (12 - 4)\n",
    "5. a = 4, b = 4 (8 - 4)\n",
    "6. a = 0 (4 - 4), b = 4\n",
    "6. return 4\n",
    "
" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_4\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_4\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_4\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_4\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #4:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #4 state: Loading...
\n", "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "int gcd(int a, int b) {\n", " if (a == 0) // base case\n", " return b;\n", " else if (b == 0) // base case\n", " return a;\n", " else if (a > b)\n", " return gcd(a - b, b);\n", " else // \n", " return gcd(a, b - a);\n", "}\n", "\n", "void setup() {\n", " println(\"gcd(8, 12): \" + gcd(8, 12));\n", " println(\"gcd(3, 2): \" + gcd(3, 2));\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.3 isLeapyear" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_5\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_5\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_5\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_5\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #5:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #5 state: Loading...
\n", "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "boolean isLeapyear(int year) {\n", " // if a year is evenly divided by 100\n", " // but it is not evenly divided by 400\n", " // it is a leapyear\n", " // else if a year is evenly divided by 4, it is a leap year\n", " // otherwise, not a leapyear\n", " return true;\n", "}\n", "\n", "void setup() {\n", " println(\"isLeapyear(2000): \" + isLeapyear(2000) + \" (should be true)\");\n", " println(\"isLeapyear(1904): \" + isLeapyear(1904) + \" (should be true)\");\n", " println(\"isLeapyear(1900): \" + isLeapyear(1900) + \" (should be false)\");\n", " println(\"isLeapyear(1901): \" + isLeapyear(1901) + \" (should be false)\");\n", "}" ] } ], "metadata": { "kernelspec": { "display_name": "Calysto Processing", "language": "java", "name": "calysto_processing" }, "language_info": { "codemirror_mode": { "name": "text/x-java", "version": 2 }, "file_extension": ".java", "mimetype": "text/x-java", "name": "java" } }, "nbformat": 4, "nbformat_minor": 0 }